package com.ktjy.service.impl;

import com.ktjy.entity.SysUser;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> findListUser(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }


    @Override
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int row = 0;
        for (int i = 0; i < sysUserList.size(); i++) {
            SysUser sysUser = sysUserList.get(i);
            if (i == 1) {
//                插入到第二天数据时,模拟数据库异常,查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            //计入受影响的总行数
            row += sysUserMapper.insertSysUser(sysUser);
        }
        return row;
    }

    @Override
    public int delUserById(Long id) {
//        if (true) {
//                throw new RuntimeException("数据库异常");
//        }
        return sysUserMapper.deleteSysUser(id);
    }

    @Override
    public int updateUser(SysUser sysUser) {
//                if (true) {
//                throw new RuntimeException("数据库异常");
//        }
        return sysUserMapper.updateSysUser(sysUser);
    }

    // 注入SysUserMapper
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
}
